/*
 * Copyright (C) 2015 Karumi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.karumi.dexter.listener;

/**
 * If a permission was denied, an instance of this class will be returned
 * in the callback.
 */
public final class PermissionDeniedResponse {
    private final PermissionRequest requestedPermission;
    private final boolean permanentlyDenied;

    public PermissionDeniedResponse(PermissionRequest requestedPermission, boolean permanentlyDenied) {
        this.requestedPermission = requestedPermission;
        this.permanentlyDenied = permanentlyDenied;
    }

    /**
     * Builds a new instance of PermissionDeniedResponse from a given permission string
     * and a permanently-denied boolean flag
     * @param permission 单个权限
     * @param permanentlyDenied 是否拒绝
     * @return PermissionDeniedResponse
     */
    public static PermissionDeniedResponse from(String permission, boolean permanentlyDenied) {
        return new PermissionDeniedResponse(new PermissionRequest(permission), permanentlyDenied);
    }

    /**
     * 获取权限集合
     * @return PermissionRequest
     */
    public PermissionRequest getRequestedPermission() {
        return requestedPermission;
    }

    /**
     * 获取权限名
     * @return String
     */
    public String getPermissionName() {
        return requestedPermission.getName();
    }

    /**
     * 权限是否已拒绝
     * @return boolean
     */
    public boolean isPermanentlyDenied() {
        return permanentlyDenied;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        PermissionDeniedResponse that = (PermissionDeniedResponse) obj;
        return requestedPermission.getName().equals(that.requestedPermission.getName());
    }

    @Override
    public int hashCode() {
        return requestedPermission.getName().hashCode();
    }
}
